<?php
	session_start();

	if(!isset($_SESSION['id'])){
		header('Location:login.php');
	}
	if(isset($_POST['submit'])){
		$xmname=$_POST['xmname'];
		$ename=$_POST['ename'];
		$manage=$_POST['manage'];

		if($xmname==""||$ename==""){
			echo "<script>alert('名称不能为空');history.go(-1);</script>";
		}elseif(!preg_match("/^[\x{4e00}-\x{9fa5}]{2,5}+$/u", $xmname)){
			echo "<script>alert('名称格式不正确，由中文且长度为2~5个字符');history.go(-1);</script>";
		}elseif(!preg_match("/^[a-zA-Z]+$/", $ename)){
			echo "<script>alert('英文名称格式不正确(最好为其中文对应的名称)');history.go(-1);</script>";
		}else{
			/*$connect=mysql_connect('localhost','root','');
			mysql_select_db('shenji',$connect);
			mysql_query("set names utf8");
			$sql2="select name from type where name='$xmname'";
			$result2=mysql_query($sql2);
			$row2=mysql_num_rows($result2);*/
			$db=new PDO("mysql:host=localhost;dbname=shenji","root","");
			$db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
			$db->exec("set names utf8");
			$sql2="select count(*) from type where name=?";
			$result2=$db->prepare($sql2);
			$exeres=$result2->execute(array($xmname));
			if($exeres){
				$row2=$result2->fetch(PDO::FETCH_NUM);
			}
			if($row2[0]){
				echo "<script>alert('该项目名称已经存在');history.go(-1);</script>";
			}else{
				/*$sql="insert into type(name,ename,last_version,manage)values('$xmname','$ename',100,'$manage')";
				$result=mysql_query($sql);*/
				$sql="insert into type(name,ename,last_version,manage)values(?,?,?,?)";
				$result=$db->prepare($sql);
				$result->execute(array($xmname,$ename,100,$manage));				
				if($result){
				mkdir('./upload/'.$xmname);
				chmod('./upload/'.$xmname, 0777);
				echo "<script>alert('添加成功');location.href='select_xiangmu.php';</script>";					
				}else{
				echo "<script>alert('添加失败');history.go(-1);</script>";	
				}

			}
		}
		
	}
?>